<template>
    <base-dialog title="发文号" width="700px" v-bind="$attrs" v-on="$listeners"  top="100px" ref="my-dialog" @submit="toSubmit">
        <el-form ref="myForm" :model="formData" label-width="80px" :rules="rules" :inline="true">
            <el-form-item label="发文字" prop="fwz">
                <el-autocomplete
                        class="inline-input"
                        v-model="formData.fwz"
                        :fetch-suggestions="querySearch"
                        placeholder="请输入发文字"
                        style="width: 220px"
                        @select="selectFwz"
                ></el-autocomplete>
                [
                <el-input v-model="formData.year" placeholder="" style="width: 70px"
                          class="fwh"></el-input>
                ]
                <el-input v-model="formData.fwh" placeholder=""  style="width: 80px"
                          class="fwh"></el-input>
                号
            </el-form-item>
        </el-form>
    </base-dialog>
</template>
<script>
    import BaseDialog from "@/components/Common/BaseDialog";
    import schoolDocuments from "@/api/officialDocument/schoolDocuments/index"
    import schoolSignature from "@/api/officialDocument/schoolSignature/index"
    export default {
        name:"EditFwhDialog",
        components: { BaseDialog},
        props:["row"],
        data(){
            return {
                formData:{
                    year: new Date().getFullYear()+"",
                    fwh: "",
                    fwz: "",
                },
                rules: {
                    fwz: [{
                        required: true,
                        message: '请选择发文字',
                        trigger: 'change'
                    },{
                        validator: this.checkFwh,
                        message: '请填写完整信息',
                        trigger: 'change'
                    }],
                },
                isEdit:true,
            }
        },
        created() {
        },
        computed:{
          uploadUrl(){
              return "/api/document/"+this.row.id+"/upload_th/"
          },
        },
        methods:{
            selectFwz(item){
                this.getDocumentFwh()
            },
            getDocumentFwh(){
                schoolSignature.getDocumentFwh({type_id:this.row.type_id,id:this.row.id,name:this.formData.fwz}).then(res => {
                   this.formData.fwh = res.data.fwh
                })
            },
            checkFwh(rule, value, callback){
                if (value&&this.formData.year&&this.formData.fwz) {
                    callback();
                } else {
                    callback(new Error(`请输入发文号`));
                }
            },
            toSubmit(is_draft){
                this.$refs['myForm'].validate(valid => {
                    if (!valid) return
                    schoolSignature.getDocumentFwhPost({type_id:this.row.type_id,id:this.row.id,...this.formData}).then(res => {
                        this.$emit("callback",this.formData)
                    })
                })
            },
            querySearch(queryString, cb) {
                schoolDocuments.getDocumentCategory({search_value:queryString,type:this.row.type_id,dept:this.row.dept_id}).then(res => {
                    this.fwhList = res.data.map(item=>{
                        return {value:item.name}
                    })
                    cb(this.fwhList);
                })
            },
            toClose(){
                this.$refs["my-dialog"].handleClose()
            },
        }
    }
</script>
<style lang="scss" scoped>
    ::v-deep.elformBox .el-dialog__body {
        .el-form-item{
            margin-bottom: 0px;
        }
    }

</style>
